package IO.stream;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.Vector;

public class RWTextBase {
	
	Vector<Info> listInfo;
	
	public RWTextBase() {
		listInfo = new Vector<Info>();
	}
	
	/* Read info from file into a new list*/
	public void readInfo(String filename){
		BufferedReader br = null;
		String line = null;
		listInfo = new Vector<Info>();
		try {
			br = new BufferedReader(new FileReader(filename));
			while ((line=br.readLine()) != null) {
				StringTokenizer stok = new StringTokenizer(line, ";");
				Info ninfo = new Info(stok.nextToken(), Integer.parseInt(stok.nextToken()), Double.parseDouble(stok.nextToken()));
				listInfo.add(ninfo);
			}
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/* Write all info of list to file*/
	public void writeInfo(String filename){
		BufferedWriter bw = null;
		try {
			bw = new BufferedWriter(new FileWriter(filename));
			for (Info info : listInfo){
				bw.write(info.toString());
				bw.newLine();
			}
				
			bw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/* Add info test*/
	public void addInfo(Info info){
		listInfo.add(info);
	}
	
	
	public void display(){
		for (Info info : listInfo)
			System.out.println(info.toString());
	}
	
	public static void main(String[] args) {
//		RWTextBase tester = new RWTextBase();
//		Info i1 = new Info("Info1", 111, 1111.1111);
//		Info i2 = new Info("Info2", 222, 2222.2222);
//		Info i3 = new Info("Info3", 222, 3333.3333);
//		tester.addInfo(i1);
//		tester.addInfo(i2);
//		tester.addInfo(i3);
//		
//		tester.writeInfo("src/IO/stream/file.txt");
		
		RWTextBase tester2 = new RWTextBase();
		tester2.readInfo("src/IO/stream/file.txt");
		tester2.display();
	}
}
